#include "Pile.h"

void Initialize(pile &p);
void Display(const pile &p);

//Initialize the pile by setting each of its parameters with the specified arguments.
//Pretty straightforward
void Initialize(pile& p, pileType type, int xPos, int yPos, int id)
{
	p.xPos = xPos;
	p.yPos = yPos;
	p.type = type;
	p.id = id;
	p.nb = 0;
//	Populate(p); //------------------------------
}

//This is a debug function
void Populate(pile& p)
{
	for (int i = 0; i < 5; i++)
	{
		card c;
		Initialize(c, char(3), 2, true);
		p.cards[p.nb] = c;
		p.nb += 1;
	}
}


//Displays a pile, different methods are used for the different pile types since they're all displayed in different ways.
//Stacks shows an empty pile when empty.
//Columns and Devils do not show anything when empty.
//
//Stacks only show the card on the top of the stack.
//Columns and Devils show all their contained cards but in different manners.
void Display(const pile& p)
{
	if (p.type == STACK)
	{
		//The stack's ID is shown above itself
		PlaceChar(char(49 + p.id), p.xPos + 2, p.yPos - 1);
		if (p.nb != 0)
		{
			Display(p.cards[p.nb - 1], p.xPos, p.yPos);
		}
		else
		{
			Display(EMPTY_PILE, p.xPos, p.yPos, true);
		}
	}
	else if (p.type == COLUMN)
	{
		//The column's ID is shown above itself even if the column is empty.
		PlaceChar(char(49 + p.id), p.xPos + 2, p.yPos - 1);
		if (p.nb != 0)
		{
			for (int i = 0; i < p.nb; i++)
			{
				//If we ever get more than 10 cards in the same column (the highest possible limit being 19)...
				//Cards following the 10th will be displayed side-by-side.
				if (i < 10)
				{
					// If we're dealing with the 10th card and there is less than 17 cards to display, show the full version of it.
					//This is done for aesthetic purposes, everyone like sexy cards.
					if (i == 9 && p.nb < 17)
					{
						Display(p.cards[i], p.xPos, p.yPos + i, false, false);
					}
					else
					{
						Display(p.cards[i], p.xPos, p.yPos + i, false, i != p.nb - 1);
					}
				}
				else
				{
					Display(p.cards[i], p.xPos + COLUMN_OFFSET_X, p.yPos + i - COLUMN_OFFSET_Y, false, i != p.nb - 1);
				}
			}
		}
	}
	//There's no poing going in there if the devil stack is empty since it's a one-way out unlike columns.
	else if (p.type == DEVIL && p.nb != 0)
	{
		for (int i = 0; i < p.nb; i++)
		{
			Display(p.cards[i], p.xPos, p.yPos + i * 3);
		}

	}

}

